<?php

class SessionDelegate
{
    /**
     * @var    array
     * @access private
     */
    var $store;

    /**
     * @param  string   $session_name
     * @return object
     * @access public
     */
    function getInstance($session_name = '')
    {
        static $instance;
        if (!isset($instance)) {
            $instance =& new SessionDelegate($session_name);
        }
        return $instance;
    }

    /**
     * @param  string   $key
     * @return mixed
     * @access public
     */
    function get($key)
    {
        return (isset($this->store[$key]))? $this->store[$key]: false;
    }

    /**
     * @param  string   $key
     * @param  mixed    $value
     * @return void
     * @access public
     */
    function set($key, $value)
    {
        $this->store[$key] = $value;
    }

    /**
     * @param  string   $session_name
     * @return void
     * @access private
     */
    function SessionDelegate($session_name = '')
    {
        if (!empty($session_name)) {
            session_name($session_name);
        }
        $cookie_path = pathinfo($_SERVER['SCRIPT_NAME'], PATHINFO_DIRNAME);
        session_set_cookie_params(0, $cookie_path);
        session_cache_limiter('nocache');
        @session_start();
        $this->store =& $_SESSION;
        $identifier = md5((isset($_SERVER['HTTP_USER_AGENT'])? $_SERVER['HTTP_USER_AGENT']: ''));
        if ($current_identifier = $this->get('identifier')
            and $current_identifier != $identifier) {
            $this->store = array();
            if (isset($_COOKIE[session_name()])) {
                setcookie(session_name(), '', time() -42000, $cookie_path);
            }
           session_regenerate_id(true);
        }
        $this->set('identifier', $identifier);
    }
}
